package com.ruoyi.teaching.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.CourseDesignMapper;
import com.ruoyi.teaching.domain.CourseDesign;
import com.ruoyi.teaching.service.ICourseDesignService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程设计Service业务层处理
 *
 * @author sunshine
 * @date 2024-01-26
 */
@Service
public class CourseDesignServiceImpl implements ICourseDesignService
{
    @Autowired
    private CourseDesignMapper courseDesignMapper;

    /**
     * 用id选择课程设计
     * 查询课程设计
     *
     * @param stageId 阶段id
     * @return 课程设计
     */
    @Override
    @Transactional(readOnly = true)
    public List<CourseDesign> selectCourseDesignById(Long stage)
    {

        return courseDesignMapper.selectCourseDesignBystageId(stage);
    }

    /**
     * 查询课程设计列表
     *
     * @param courseDesign 课程设计
     * @return 课程设计
     */
    @Override
    public List<CourseDesign> selectCourseDesignList(CourseDesign courseDesign)
    {
        return courseDesignMapper.selectCourseDesignList(courseDesign);
    }

    /**
     * 新增课程设计
     *
     * @param courseDesign 课程设计
     * @return 结果
     */
    @Override
    public int insertCourseDesign(CourseDesign courseDesign)
    {
        courseDesign.setCreateTime(DateUtils.getNowDate());
        return courseDesignMapper.insertCourseDesign(courseDesign);
    }

    /**
     * 修改课程设计
     *
     * @param courseDesign 课程设计
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateCourseDesign(List<CourseDesign> courseDesign)
    {
        if (CollectionUtils.isEmpty(courseDesign)){
            throw new ServiceException("课程设计不能为空");
        }

//        删除
        String stage = courseDesign.get(0).getStage();
        List<Long> collect = courseDesign
                .stream()
                .map(CourseDesign::getId)
                .filter(ObjectUtils::isNotEmpty)
                .collect(Collectors.toList());
        for (CourseDesign c:courseDesign){
            if (c.getId()!=null){
                courseDesignMapper.deleteCourseDesignByNotIds(stage,collect);
            }
        }

//        遍历
        courseDesign.forEach(courseDesign1 -> {
            if (ObjectUtils.isNotEmpty(courseDesign1.getId())){
                courseDesign1.setUpdateBy(SecurityUtils.getUsername());
                courseDesign1.setUpdateTime(new Date());
                courseDesignMapper.updateCourseDesign(courseDesign1);
            }else {
                courseDesign1.setCreateBy(SecurityUtils.getUsername());
                courseDesign1.setCreateTime(new Date());
                courseDesignMapper.insertCourseDesign(courseDesign1);
            }

        });
        return 1;
    }


    /**
     * 删除课程设计信息
     *
     * @param id 课程设计主键
     * @return 结果
     */
    @Override
    public int deleteCourseDesignById(Long id)
    {
        return courseDesignMapper.deleteCourseDesignById(id);
    }

    @Override
    public int updateRelatedInterview(Long cdId, List<Long> interviewIdList) {
//        删除之前的关联
        courseDesignMapper.deleteCourseDesignByInterview(cdId);
        return courseDesignMapper.insertCourseDesignInterview(cdId,interviewIdList);
    }
}
